<!--
 * @Descripttion: 授权文章
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-28 17:45:19
-->
<template>
  <div class="lb-app-article-sign">
    <top-nav></top-nav>
    <div class="page-main">
      <lb-tips>
        <p>获客文章分配名额总数量: {{totalNum}}; 已分配数量: {{hasNum}};</p>
        <p>可以分配给同一个员工或者多个员工, 每分配一次, 员工将增加一年的获客文章的使用时间, 多次分配同一个员工使用时间将累加;</p>
      </lb-tips>
      <el-row class="page-search-form">
        <el-form @submit.native.prevent :inline="true" :model="searchForm" ref="searchForm">
          <el-form-item label="员工姓名" prop="nameForSearch">
            <el-input v-model="searchForm.nameForSearch" placeholder="请输入员工姓名"></el-input>
          </el-form-item>
          <el-form-item label="员工昵称" prop="nickNameFormSearch">
            <el-input v-model="searchForm.nickNameForSearch" placeholder="请输入员工昵称"></el-input>
          </el-form-item>
          <el-form-item>
            <lb-button size="medium" type="primary" icon="el-icon-search" style="margin-right:5px" @click="getTableDataList(1)">{{$t('action.search')}}</lb-button>
            <lb-button size="medium" icon="el-icon-refresh-left" style="margin-right:5px" @click="resetForm('searchForm')">{{$t('action.reset')}}</lb-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
        style="width: 100%"
      >
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="nickName" label="员工昵称"></el-table-column>
        <el-table-column label="头像">
          <template slot-scope="scope">
            <lb-image :src="scope.row.avatar"></lb-image>
          </template>
        </el-table-column>

        <el-table-column prop="end_time" label="获客文章到期时间"></el-table-column>
        <el-table-column prop="status" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                type="primary"
                size="mini"
                plain
                @click="updateCustomerArticle(scope.row.id)"
              >授权获客文章</lb-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      ></lb-page>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      searchForm: {
        page: 1,
        limit: 10,
        nameForSearch: '',
        nickNameForSearch: '',
        page_count: ''
      },
      tableData: [],
      loading: false,
      total: 0,
      totalNum: '',
      hasNum: ''
    }
  },
  created () {
    this.getTableDataList()
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    getTableDataList (flag) {
      this.loading = true
      let { searchForm } = this
      if (flag) this.searchForm.page = 1
      searchForm.page_count = searchForm.limit
      this.$api.getCustomerArticleList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
          this.totalNum = res.data.total_year
          this.hasNum = res.data.used_year
        }
      })
    },
    updateCustomerArticle (id) {
      this.$confirm(
        '是否消耗一次获客文章数量授权该员工获客文章? 每授权一次该员工将增加一年的获客文章使用时间, 同一个员工可以重复授权多次, 该操作不可撤回！',
        '温馨提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          this.$api
            .updateCustomerArticle({
              id
            })
            .then(res => {
              if (res.code === 200) {
                this.$message.success(this.$t('tips.successOper'))
                this.getTableDataList()
              }
            })
        })
        .catch(() => {})
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-app-article-sign {
  .el-select,
  .el-input {
    width: 200px;
  }
}
</style>
